package com.bsoft.gol.hcs.specialist.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.bsoft.gol.hcs.satisfaction.bean.request.SurveyDealWithPlanDetailRequest;
import com.bsoft.gol.hcs.satisfaction.bean.request.SurveyDealWithPlanListRequest;
import com.bsoft.gol.hcs.satisfaction.bean.response.SurveyDealWithPlanDetailResponse;
import com.bsoft.gol.hcs.satisfaction.bean.response.SurveyDealWithPlanListResponse;
import com.bsoft.gol.hcs.satisfaction.dao.PatientSatisfactionSurveyDAO;
import com.bsoft.gol.hcs.satisfaction.service.SatisfactionSurveyService;
import com.bsoft.gol.hcs.specialist.bean.vo.SurveyDealWithPlanListVO;
import com.bsoft.gol.hcs.utils.DateUtils;
import com.bsoft.gol.hcs.visitrecord.dao.VrInpatientVisitRecordDAO;
import com.bsoft.gol.hcs.visitrecord.dao.VrOutpatientVisitRecordDAO;
import com.bsoft.gol.hcs.visitrecord.entity.VrInpatientVisitRecord;
import ctd.mvc.controller.OutputSupportMVCController;
import hcn.base.VrOutpatientVisitRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


/**
 * 满意度
 *
 **/
@Controller
@RequestMapping("/api/auth/satisfactionSurvey/")
@Slf4j
public class SatisfactionSurveyController extends OutputSupportMVCController{

    @Autowired
    private SatisfactionSurveyService satisfactionSurveyService;
    @Autowired
    private VrInpatientVisitRecordDAO vrInpatientVisitRecordDAO;
    @Autowired
    private VrOutpatientVisitRecordDAO vrOutpatientVisitRecordDAO;
    @Autowired
    private PatientSatisfactionSurveyDAO patientSatisfactionSurveyDAO;

    /**
     * 满意度处理列表导出
     */
    @PostMapping(value = "dealWithPlanListExport")
    public void surveyDealWithPlanListExport(@RequestBody SurveyDealWithPlanListRequest request, HttpServletResponse response) throws Exception {
        log.info("request={}", JSONObject.toJSONString(request));
        SurveyDealWithPlanListResponse surveyResponse = satisfactionSurveyService.querySurveyDealWithPlanList(request);
        List<SurveyDealWithPlanListVO> voList = new ArrayList<>();
        for (SurveyDealWithPlanListResponse.DealWithPlanBean resp : surveyResponse.getList()) {
            SurveyDealWithPlanListVO vo = new SurveyDealWithPlanListVO();
            if("1".equals(resp.getRecordType())){
                VrOutpatientVisitRecord outRecord = vrOutpatientVisitRecordDAO.get(resp.getBusinessId());
                if(outRecord!=null){
                    vo.setVisitTime(DateUtils.dateFormate(outRecord.getStartTime(), "yyyy-MM-dd HH:mm"));
                }
            }else if("2".equals(resp.getRecordType())){
                VrInpatientVisitRecord inRecord = vrInpatientVisitRecordDAO.get(resp.getBusinessId());
                if(inRecord!=null){
                    vo.setVisitTime(DateUtils.dateFormate(inRecord.getAdmissionTime(), "yyyy-MM-dd HH:mm"));
                }
            }
            vo.setSurveyTime(DateUtils.dateFormate(resp.getSurveyTime(), "yyyy-MM-dd HH:mm:ss"));
            vo.setDepartmentName(resp.getDepartmentName());
            vo.setPatientName(resp.getPatientName());
            
            StringBuffer processingDoctorName = new StringBuffer();
            StringBuffer processingOpinions = new StringBuffer();
            SurveyDealWithPlanDetailRequest req = new SurveyDealWithPlanDetailRequest();
            req.setDocId(request.getDocId());
            req.setPatientSurveyId(resp.getPatientSurveyId());
            req.setQuestionnaireId(request.getQuestionnaireId());
            req.setProcessingStatus("2");
            List<SurveyDealWithPlanDetailResponse.TitleBean> dealWithPlanList =
                    patientSatisfactionSurveyDAO.getSurveyDealWithPlanDetail(req);
            if(!CollectionUtils.isEmpty(dealWithPlanList)){
                for (int i=0; i<dealWithPlanList.size(); i++){
                    SurveyDealWithPlanDetailResponse.TitleBean titleBean = dealWithPlanList.get(i);
                    processingDoctorName.append(titleBean.getProcessingDoctorName());
                    processingOpinions.append(titleBean.getProcessingOpinions());
                    if(i<dealWithPlanList.size()-1){
                        processingDoctorName.append("、");
                        processingOpinions.append("、");
                    }
                }
            }
            vo.setProcessingDoctorName(processingDoctorName.toString());
            vo.setProcessingOpinions(processingOpinions.toString());

            voList.add(vo);
        }
        log.info("voList={}", JSONObject.toJSONString(voList));

        //导出excel  设置响应类型和编码
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("满意度处理列表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SurveyDealWithPlanListVO.class).sheet("导出数据").doWrite(voList);
    }


}
